<template>
  <div class="AddOrUpdate_main add_container add_container_table column_three" :class="showKey.type">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <div
          v-for="(item2,index2) in showKey.keys"
          :key="'index2'+index2"
          :class="getClass(item2)"
        >
          <el-form-item v-if="item2.type=='br'" class="showItem_br" />
          <el-form-item v-if="item2.type=='form'" :label="item2.name">
            <div v-if="!showData[item2.key]&&showData[item2.key]!==0&&showData[item2.key]!=='0'" class="show noData">暂无</div>
            <div v-else-if="item2.key=='status'" class="show">{{ showData[item2.key]=='1'||showData[item2.key]==1?'启用':'停用' }}</div>
            <div v-else class="show">{{ showData[item2.key] }}</div>
          </el-form-item>
          <el-form-item v-if="item2.type =='file'" :label="item2.name">
            <FileManageFull
              :file-names="formDataObj.fileName"
              :file-real-names="formDataObj.fileRealName"
              :file-urls="formDataObj.url"
              :row-index="1"
              :readonly="!canEditFile"
              @fileName="setFileName"
            />
          </el-form-item>

          <div v-if="item2.type=='table'" class="formItem_header">
            {{ item2.name }}
          </div>
          <div v-if="item2.type=='table'" ref="table_container" class="table_container">
            <el-table
              ref="table"
              :data="showData[item2.key]"
              :height="tableHeight"
              stripe
              border
              row-key="id"
              :summary-method="getSummaries"
              :show-summary="true"
            >
              <el-table-column
                v-for="(item3,index3) in item2.columns"
                :key="index3"
                :label="item3.name"
                :prop="item3.key"
                :min-width="item3.minWidth||100"
                :show-overflow-tooltip="true"
                :fixed="item3.fixed"
                :align="item3.align"
              >
                <template slot-scope="scope">
                  <div
                    v-if="item3.type=='column'"
                    style=" white-space:nowrap; text-overflow:ellipsis; overflow:hidden;"
                  >{{ scope.row[item3.key] }}</div>
                  <FileManage
                    v-if="item3.type=='file'"
                    :file-names.sync="scope.row[item3.fileName]"
                    :file-real-names.sync="scope.row[item3.fileRealName]"
                    :file-urls.sync="scope.row[item3.fileUrl]"
                    :row-index="scope.$index"
                    :readonly="true"
                    @fileName="setFileName"
                  />
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>

      </el-form>
    </div>
    <div class="main_bottom">
      <div>
        <el-button v-if="showData.status==1" @click.stop="delivery">已发货</el-button>
        <el-button v-if="canEditFile" @click.stop="preserve">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Big from 'big.js' // big.js解决精度丢失问题
import {
  addOrUpdate, updateFile
} from '@/api/purchaseManage/purchaseOrder'
export default {
  name: 'ShowDetailOld',
  props: {
    layerid: {},
    id: {},
    showKey: {},
    showData: {}
  },
  data() {
    return {
      formData: {
      },
      formDataObj: {
        id: '',
        fileName: '',
        fileRealName: '',
        url: ''
      },
      canEditFile: false, // 是否能够修改附件
      tableHeight: '',
      rules: {
      }
    }
  },
  updated() {
    this.$nextTick(() => {
      this.$refs['table'].doLayout()
    })
  },
  mounted() {
    if (this.showData.detailList.length > 7) {
      this.tableHeight = '390'
    } else {
      this.tableHeight = ''
    }
    Object.keys(this.formDataObj).forEach((key) => {
      // 给右边数据赋值
      this.formDataObj[key] = this.showData[key]
    })
    if (!(this.showData.forInspect == 0 && this.showData.status < '2')) {
      this.canEditFile = true
    }
    this.$refs['table'].doLayout()
  },
  methods: {// 表格拖动排序
    // 合计行
    getSummaries(param) {
      const {
        columns,
        data
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'purchaseNumber':
            sums[index] = data.map(item => item.purchaseNumber).reduce((prev, curr) => {
              if (curr) {
                return new Big(prev).plus(curr).toString()
              } else {
                return new Big(prev).toString()
              }
            }, 0)
            this.formData.totalNumber = sums[index]
            break
        }
        switch (column.property) {
          case 'totalTaxPriceThousandths':
            sums[index] = data.map(item => item.totalTaxPrice).reduce((prev, curr) => {
              if (curr) {
                return new Big(prev).plus(curr).toString()
              } else {
                return new Big(prev).toString()
              }
            }, 0)
            this.formData.totalTaxPriceThousandths = sums[index]
            break
        }
      })
      return sums
    },

    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    setFileName(...data) {
      this.formDataObj.fileName = data[0]
      this.formDataObj.fileRealName = data[1]
      this.formDataObj.url = data[2]
    },
    // 提交
    preserve() {
      updateFile(this.formDataObj).then((res) => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.$layer.close(this.layerid) // 关闭弹框
        this.$parent.getList() // 调用父组件的刷新方法
      })
    },
    // 发货
    delivery() {
      Object.keys(this.formDataObj).forEach((key) => {
      // 给右边数据赋值
        this.showData[key] = this.formDataObj[key]
      })
      const _this = this
      this.$confirm('确定已发货了吗', '发货确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      })
        .then(function() {
          addOrUpdate({ ..._this.showData, status: 4 }).then((res) => {
            _this.$message({
              message: res.msg,
              type: 'success'
            })
            _this.$layer.close(_this.layerid) // 关闭弹框
            _this.$parent.getList() // 调用父组件的刷新方法
          })
        })
        .catch(() => {})
    },
    getClass(item) {
      let res = ''
      if (item.type == 'form' || item.type == 'br') {
        res += 'showItem'
        if (item.length && item.length == 'double') {
          res += ' double'
        }
      } else if (item.type == 'file') {
        res += 'showItem double'
      } else {
        res += 'showTable'
      }

      return res
    }
  }
}
</script>

<style lang="less" scoped>

.main_main{
  padding-top: 0px;
  .el-form{
    height: 100%;
    align-content: flex-start;
    .showItem{
      margin-left: 20px;
      margin-right: 20px;
      width: calc(33.33% - 40px);
    }

    /deep/ .el-form-item{
      width: 100%;
      border-bottom: 1px solid #D6D6D6;
      margin-left: 0;
      margin-right: 0;
      margin-bottom: 0;
      .el-form-item__label:before{
        content: none;
      }
      .el-form-item__label{
        color: #1F51B3;
        font-size: 15px;
      }
      .el-form-item__label{
        width: 140px !important;
        margin-top: 10px;
      }
      .el-form-item__content{
        word-break: break-all;
        color: #333333;
        font-size: 15px;
        flex: 1;
        width: unset;
        margin-right: 0px;
      }
    }
    .double{
      width: calc(100% - 40px);
      //height: auto;
      ///deep/ .el-form-item__content{
      //  height: auto;
      //}
      ///deep/ .el-form-item{
      //  height: auto;
      //}
      //.show{
      //  overflow: hidden;
      //  text-overflow: ellipsis;
      //  display: -webkit-box;
      //  -webkit-line-clamp: 2;
      //  -webkit-box-orient: vertical;
      //}
    }
    .showTableTwo {
      width: 100% !important;
      /deep/  .el-form-item{
        margin-left: 10px !important;
        padding-right: 10px !important;
      }
      /deep/ .table_container{
        padding-bottom: 20px !important;
      }
    }
    .showTable{
      width: 100%;
      /deep/  .el-form-item{
        margin-left: 20px !important;
        margin-right: 20px !important;
        width: auto;
      }
    }
  }
  .column_one{
    .showItem{
      width: 100% !important;
    }
  }
  /deep/ .el-tab-pane{
    align-content: flex-start;
  }
}

.column_one{
  .showItem{
    width: 100% !important;
  }
}
.noData{
  color: #c0c4cc;
}
.show{
  height: fit-content !important;
  padding-top: 10px;
  padding-bottom: 10px;
}
.FileManageFull_container{
  padding-top: 10px;
  padding-left: 10px;
}
.showItem_br{
  border-bottom: 0 !important;
}
.formItem_header{
  font-size: 18px;
  width: 100%;
  height: 50px;
  line-height: 50px;
  padding-left: 20px;
  padding-right: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.table_container {
  padding-bottom: 20px !important;
  padding-top: 0px !important;
}
.el-table {
  font-size: 15px;
}
/deep/ .el-table__fixed-footer-wrapper {
  .el-table__cell:last-of-type {
    height: 39px !important;
  }
}
</style>
